package com.zcj.demo.web.user.rest;

import com.zcj.demo.user.service.AccountService;
import com.zcj.demo.user.service.dto.UserDTO;
import com.zcj.demo.user.service.mapper.UserMapper;
import com.zcj.demo.utils.RedisUtils;
import com.zcj.demo.utils.TokenUtil;
import com.zcj.demo.web.errors.AuthException;
import com.zcj.demo.web.user.vo.LoginVO;
import com.zcj.demo.web.user.vo.StatusVO;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author 68300119
 * @Date 2020/10/13
 **/
@RestController
@RequestMapping("/api")
public class AccountController {

    private final AccountService accountService;
    private final RedisUtils redisUtils;
    private final UserMapper userMapper;

    public AccountController(AccountService accountService, RedisUtils redisUtils, UserMapper userMapper) {
        this.accountService = accountService;
        this.redisUtils = redisUtils;
        this.userMapper = userMapper;
    }

    @GetMapping("/login/captcha")
    public ResponseEntity<Void> getCaptcha(String mobile) {
        accountService.getCaptcha(mobile);
        return ResponseEntity.ok().build();
    }

    /**
     * 登录
     *
     * @param params
     * @return
     */
    @PostMapping(value = "/login/account")
    public ResponseEntity<Object> login(@RequestBody LoginVO params) {
        String token = accountService.login(params);
        return ResponseEntity.ok(new StatusVO("ok", token));

    }

    /**
     * 退出
     *
     * @param request
     * @return
     */
    @GetMapping("/logout")
    public ResponseEntity<Object> logout(HttpServletRequest request) {
        try {
            String token = request.getHeader("Authorization").replace("Bearer ", "");
            if (StringUtils.isNotBlank(token)) {
                Map<String, Object> map = TokenUtil.getUserInfoFromToken(token);
                if (ObjectUtils.isEmpty(map)) {
                    return ResponseEntity.ok().build();
                }
                redisUtils.del("login:" + map.get("username").toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        SecurityContextHolder.getContext().setAuthentication(null);
        return ResponseEntity.ok().build();
    }

    /**
     * 当前用户
     *
     * @param request
     * @return
     */
    @GetMapping("/currentUser")
    public ResponseEntity<UserDTO> currentUser(HttpServletRequest request) {
        String token = request.getHeader("Authorization").replace("Bearer ", "");
        if (StringUtils.isNotBlank(token)) {
            String username = (String) TokenUtil.getUserInfoFromToken(token).get("username");
            UserDTO userDTO;
            try {
                userDTO = (UserDTO) redisUtils.get("login:" + username);
                userDTO.setPassword(null);
            } catch (Exception e) {
                throw new AuthException();
            }
            return ResponseEntity.ok(userDTO);
        } else {
            throw new AuthException();
        }
    }

    /**
     * 注册
     *
     * @param user 用户信息
     * @return 用户信息
     */
    @PostMapping("/register")
    public ResponseEntity<UserDTO> register(@RequestBody UserDTO user) {
        user.setUsername(user.getEmail());
        UserDTO result = accountService.register(user);
        result.setPassword(null);
        return ResponseEntity.ok(result);
    }
}
